import { req_singer,req_singer_info,req_singer_song,req_singer_album,req_singer_mv } from "@/api";
const state = {
    singer: {},
    singer_all: {},
    singer_1: {},

    //歌手资料介绍
    singer_info:{},
    //歌手歌曲
    singer_song:{},
    //专辑
    singer_album:{},
    //mv
    singer_mv:{}
};
const getters = {
    //处理异步问题
    enu(state) {
        return state.singer_all.enu_list || {}
    },
    all_info(state) {
        return state.singer_all.info || {}
    },
    
};
const mutations = {
    SINGER(state, singer) {
        //处理图片大小(路径问题)问题
        for (var i in singer.info) {
            singer.info[i].imgurl = singer.info[i].imgurl.replace(/{size}/, '200')
        }

        state.singer = singer

    },
    SINGER_ALL(state, singer) {
        //处理图片大小问题
        for (var i in singer.info) {
            singer.info[i].imgurl = singer.info[i].imgurl.replace(/{size}/, '200')
        }
        //处理分类数据乱序问题
        //sextypes
        var sort_name = new Array()
        for (var i in singer.enu_list.sextypes) {
            for (var j in singer.enu_list.sextypes) {
                if (singer.enu_list.sextypes[j].key == i) {
                    sort_name.push(singer.enu_list.sextypes[j])
                    break
                }
            }
        }
        singer.enu_list.sextypes = sort_name
        //types
        var types = new Array()
        for (var i in singer.enu_list.types) {
            for (var j in singer.enu_list.types) {
                if (singer.enu_list.types[j].key == i) {
                    types.push(singer.enu_list.types[j])
                    break
                }
            }
        }
        singer.enu_list.types = types

        state.singer_all = singer

    },
    SINGER_1(state, singer) {
        for (var i in singer.info) {
            singer.info[i].imgurl = singer.info[i].imgurl.replace(/{size}/, '200')
        }
        state.singer_1 = singer

    },
    //歌手介绍
    SIN_IN(state,data){
        //处理图片路径问题
        data.imgurl=data.imgurl.replace(/{size}/, '200')
        state.singer_info=data
    },
    //歌手歌曲
    SIN_SONG(state,data){
        for(var i in data.info){
            //提取专辑名
            data.info[i].filename=data.info[i].filename.split('-')[1]
        }
        state.singer_song=data
    },
    //歌手专辑
    SIN_ALBUM(state,data){
        for(var i in data.info){
            data.info[i].publishtime=data.info[i].publishtime.split(' ')[0]
            data.info[i].imgurl=data.info[i].imgurl.replace('/{size}/','/')
        }
        state.singer_album=data

    },
    SIN_MV(state,data){
        state.singer_mv=data
    }
    

};
const actions = {
    //歌手列表
    async hall_singer({ commit }, { sextype, sort, size, type }) {
        //热门歌手
        if (sort == 1) {
            if (size == 5) {
                let result = await req_singer(sextype, sort, size, type);
                if (result.status == 1) {
                    commit('SINGER', result.data)
                } else {
                    return Promise.reject(new Error('faile'))
                }
            } else {
                let result = await req_singer(sextype, sort, size, type);
                if (result.status == 1) {
                    commit('SINGER_ALL', result.data)
                } else {
                    return Promise.reject(new Error('faile'))
                }
            }

        } else {
            //飙升歌手
            if (size == 5) {
                let result = await req_singer(sextype, sort, size, type);
                if (result.status == 1) {
                    commit('SINGER_1', result.data)
                } else {
                    return Promise.reject(new Error('faile'))
                }
            } else {
                let result = await req_singer(sextype, sort, size, type);
                if (result.status == 1) {
                    commit('SINGER_ALL', result.data)
                } else {
                    return Promise.reject(new Error('faile'))
                }
            }
        }

    },
    //歌手资料数据获取
    async singer_info({ commit }, singerid) {
        let result = await req_singer_info(singerid)
        //处理返回Sring类型
        let result_1=JSON.parse(result.slice(23,-21))
        if (result_1.status == 1) {
            commit('SIN_IN', result_1.data)
        } else {
            return Promise.reject(new Error('faile'))
        }

    },
    //歌手歌曲数据获取
    async singer_song({ commit },{identify,singerid}) {
        let result = await req_singer_song(identify,singerid)
        //切割数据
        let result_1=JSON.parse(result.slice(23,-21))
        if (result_1.status == 1) {
            commit('SIN_SONG', result_1.data)
        } else {
            return Promise.reject(new Error('faile'))
        }

    },
    //歌手专辑
    async singer_album({ commit },singerid){
        let result = await req_singer_album(singerid)
        if (result.status == 1) {
            commit('SIN_ALBUM', result.data)
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    //歌手mv
    async singer_mv({ commit },singerid){
        let result = await req_singer_mv(singerid)
        let result_1=JSON.parse(result.slice(23,-21))
        if (result_1.status == 1) {
            commit('SIN_MV', result_1.data)
        } else {
            return Promise.reject(new Error('faile'))
        }
    }



};
const modules = {

};

export default {
    state,
    getters,
    mutations,
    actions,
    modules
}